/**
 * GeoSOA/Spatialytics/widgets/MdxQueryEditor.js
 *
 * MDX query editor
 *
 * Copyright (C) 2008-2009 GeoSOA Research Group, Universite Laval
 * See LICENSE file under this distribution for licensing information
 * 
 * @author Etienne Dube
 * @since 20090401
 **/


dojo.provide("GeoSOA.Spatialytics.widgets.MdxQueryEditor");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");

dojo.require("dijit.TitlePane");
dojo.require("dijit.form.SimpleTextarea");
dojo.require("dijit.form.Button");

dojo.declare("GeoSOA.Spatialytics.widgets.MdxQueryEditor",
		[dijit._Widget, dijit._Templated], {

	// title: String
	//	the title label
	title: "MDX Query Editor",
	// submitButtonLabel: String
	//	the submit button label
	submitButtonLabel: "Submit",
	// mdxQuery: String
	//	the mdx query
	mdxQuery: "SELECT ... FROM ... ",
	
	// --- private attributes:
	_observerFunc: null,
	_olapAjax: null,
	// ---
	
	templatePath: dojo.moduleUrl("GeoSOA.Spatialytics.widgets","templates/MdxQueryEditor.html"),
	widgetsInTemplate: true,

	// --- getters/setters
	
	_setTitleAttr: function (/*String*/ title) {
		this.title = title;
		this.titlePaneWidget.attr('title', this.title);
		this.titlePaneWidget.toggle();
	},
	
	_setSubmitButtonLabelAttr: function (/*String*/ label) {
		this.submitButtonLabel = label;
		this.submitButtonWidget.attr('label', this.submitButtonLabel);
	},
	
	_setMdxQueryAttr: function (/*String*/ mdx) {
		this.mdxQuery = mdx;
		this.textareaWidget.attr('value', this.mdxQuery);
	},
	
	_getMdxQueryAttr: function () {
		this.mdxQuery = this.textareaWidget.attr('value');
		return this.mdxQuery;
	},
	
	// ---
	
	
	// --- public methods
	
	submit: function () {
		if (this._olapAjax != null) {
			this._olapAjax.setMdxQuery(this.attr('mdxQuery'));
			this._olapAjax.submitQuery();
		}
	},
	
	getOlapAjaxObserver: function () {
		var that = this;
		var closure = function(olapAjax) {
			that.attr('mdxQuery', olapAjax.getMdxQuery());
		};
		return closure;
	},
	
	attachOlapAjax: function (/*GeoSOA.olap4js.OlapAjax*/ olapAjax, /*Boolean*/ observe) {
		this._olapAjax = olapAjax;
		if (observe) {
			this._observerFunc = this.getOlapAjaxObserver();
			this._olapAjax.afterResponseObservable.register(this._observerFunc);
		}
	},
	
	detachOlapAjax: function () {
		if (this._olapAjax != null) {
			if (this._observerFunc != null) {
				this._olapAjax.afterResponseObservable.unregister(this._observerFunc);
				this._observerFunc = null;
			}
			this._olapAjax = null;
			
		}
	}
	
	// ---
	
});
